package com.jiantong.work.impl.service.yangzhibaoxian;

/**
 * Created by ThinkPad on 2017/9/5.
 */

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.InformDao;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceApplicationDao;
import com.jiantong.work.model.yangzhibaoxian.HarmlessConduct;
import com.jiantong.work.model.yangzhibaoxian.Inform;
import com.jiantong.work.service.yangzhibaoxian.InformService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 消息通知表
 */
@Slf4j
@Service
@RpcProvider
public class InformServiceImpl implements InformService{
    private InformDao informSDao;

    @Autowired
    public InformServiceImpl(InformDao informSDao) {
        this.informSDao = informSDao;
    }
    @Override
    public Response<Inform> findById(Long id) {
        try {
            Inform inform = (Inform)  informSDao.findById(id);
            if (inform == null) {
                log.error("inform(id={}) is not found", id);
                return Response.fail("inform.not.found");
            }

            return Response.ok(inform);
        } catch (Exception e) {
            log.error("failed to find inform(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("inform.find.fail");
        }
    }

    @Override
    public Response<List<Inform>> findByIds(List ids) {
        try {
            List<Inform> informList =  informSDao.findByIds(ids);
            if (informList == null) {
                log.error("informList(ids={}) is not found", ids);
                return Response.fail("informList.not.found");
            }

            return Response.ok(informList);
        } catch (Exception e) {
            log.error("failed to find informList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("informList.find.fail");
        }

    }

    @Override
    public Response<List<Inform>> list(Map map) {
        try {
            List<Inform> informList =  informSDao.list(map);
            if (informList == null) {
                log.error("informList(ids={}) is not found", map);
                return Response.fail("informList.not.found");
            }

            return Response.ok(informList);
        } catch (Exception e) {
            log.error("failed to find informList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("informList.find.fail");
        }
    }

    @Override
    public Response<Paging<Inform>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<Inform> informPaging =  informSDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);
            if (informPaging == null) {
                log.error("informPaging(ids={}) is not found", map);
                return Response.fail("informPaging.not.found");
            }

            return Response.ok(informPaging);
        } catch (Exception e) {
            log.error("failed to find informPaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("informPaging.find.fail");
        }
    }

    @Override
    public Boolean update(Inform inform) {
        try {
            Boolean b =  informSDao.update(inform);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(Inform inform) {
        try {
            informSDao.create(inform);
            return inform.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<Inform> informList) {
        try {

            return  informSDao.creates(informList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }
}
